<?php

namespace app\api\logic;
use app\admin\logic\BasicLogic;
use app\shop\logic\ShopLogic;
use think\facade\Config;
use app\data\util\HttpUtil;

class TestTikTokLogic extends BasicLogic
{

    /**
     * @desc 获取会话消息测试
     * @date 2024-03-16
     */
    public function getOrders($param=[]){
        //获取TIK配置信息
        $tik_info  = Config::get('Logisticskey.' . 'TIKTOK');
        $id        = $param['orderId']??'';
        $shopId    = $param['shopId']??'';
        $shop_info = (new ShopLogic())->getShopInfoById($shopId);
        $path      = $tik_info['get_orders_path'];
        $basic_url = $tik_info['basic_url'];
        $sign = $shop_info['app_secret'] . '' . $path . 'app_key' . $shop_info['app_key'] . 'ids' . $id . 'shop_cipher' . $shop_info['cipher'] . 'timestamp' . time() . '' . $shop_info['app_secret'];
        $sign = hash_hmac('sha256', $sign, $shop_info['app_secret']);
        $url  = $basic_url . $path . "?app_key=" . $shop_info['app_key'] . "&shop_cipher=" . $shop_info['cipher'] . "&sign=" . $sign . "&timestamp=" . time() . '&ids=' . $id;
        $return = HttpUtil::tikGetUrl($url, $shop_info['access_token']);
        if($return['code']==0 && $return['data']){
            return  $this->jsonMessage($code = 200, $success = true, $msg = "获取成功", $return['data']);
        }else{
            return  $this->jsonMessage($code = 201, $success = false, $msg = "获取失败",[]);
        }

    }


    /**
     * @desc 获取会话消息测试
     * @date 2024-03-16
     */
    public function create_conversations($param=[]){
        //获取TIK配置信息
        $tik_info  = Config::get('Logisticskey.' . 'TIKTOK');
        $shopId    = $param['shopId']??'';
        $buyer_user_id = $param['buyer_user_id']??'';
        $account   = (new ShopLogic())->getShopInfoById($shopId);
        $path      = $tik_info['create_conversations_path'];
        $basic_url = $tik_info['basic_url'];
        $sign      = $account['app_secret'] . '' . $path . 'app_key' . $account['app_key'] . 'shop_cipher' . $account['cipher'] . 'timestamp' . time() . '{"buyer_user_id":"' . $buyer_user_id . '"}' . $account['app_secret'];
        $sign      = hash_hmac('sha256', $sign, $account['app_secret']);
        $url       = $basic_url . $path . "?app_key=" . $account['app_key'] . "&shop_cipher=" . $account['cipher'] . "&sign=" . $sign . "&timestamp=" . time();
        $return    = HttpUtil::tikPostUrl($url, ['buyer_user_id'=>$buyer_user_id], $account['access_token']);
        if($return['code']==0 && $return['data']){
            return  $this->jsonMessage($code = 200, $success = true, $msg = "获取成功", $return['data']);
        }else{
            return  $this->jsonMessage($code = 201, $success = false, $msg = "获取失败",[]);
        }

    }


}